/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import bean.Editora;
import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author jorge
 */
public class EditoraDao implements Serializable {
    
    private Conexao conexao;
    private PreparedStatement ps;
    private boolean sucess = false;
    
    public EditoraDao()
    {
        this.conexao = new ConexaoPostgres();
         
    }
    
    public boolean cadastrarEditora(Editora editora){
        sucess = false;
        try {
            System.out.println("EditoraDAO - Cadastro Editora: " + editora.getNome());
            ps = conexao.getConnection().prepareStatement("INSERT INTO EDITORA(nome) VALUES (?) ");
            ps.setString(1, editora.getNome());
            
            ps.executeUpdate();
            conexao.closeConnection();

            sucess = true;
        } catch (SQLException ex) {
            
            Logger.getLogger(InstituicaoDao.class.getName()).log(Level.SEVERE, null, ex);
            conexao.closeConnection();
            return sucess;
        }
        conexao.closeConnection();
        return sucess;
        
    }
    
    public boolean alterarEditora(Editora editora) {
        sucess = false;


        try {
            
            ps = conexao.getConnection().prepareStatement("UPDATE EDITORA SET nome = ? where id = ?");

            ps.setString(1, editora.getNome());
            ps.setInt(2, editora.getId());
            ps.executeUpdate();
            conexao.closeConnection();
            
            sucess = true;
                    
        } catch (SQLException ex) {
            Logger.getLogger(InstituicaoDao.class.getName()).log(Level.SEVERE, null, ex);
            conexao.closeConnection();
            return sucess;
        }


        conexao.closeConnection();
        return sucess;
    }
    
    public List<Editora> listarEditora() {
        List<Editora> editoras = new ArrayList<Editora>();
        this.ps = null;
        try {
            ps = conexao.getConnection().prepareStatement("SELECT * FROM EDITORA ORDER BY nome");
            ResultSet rs = ps.executeQuery();
            conexao.closeConnection();

            while (rs.next()) {
                Editora e = new Editora();

                e.setNome(rs.getString("nome"));
                e.setId(rs.getInt("id"));

                editoras.add(e);


            }
        } catch (SQLException ex) {
            conexao.closeConnection();
            Logger.getLogger(InstituicaoDao.class.getName()).log(Level.SEVERE, null, ex);
        }



        conexao.closeConnection();
        return editoras;

    }
    
    public Editora consultarEditora(int id) {
        this.ps = null;
        Editora e = null;
        try {
            e = new Editora();
            ps = conexao.getConnection().prepareStatement("Select * from editora where id = ?; ");
            ps.setInt(1, id);
            ResultSet rs = ps.executeQuery();
            conexao.closeConnection();
            if (rs.next()) {

                e.setNome(rs.getString("nome"));
                e.setId(id);


            }
        } catch (SQLException ex) {
            conexao.closeConnection();
            return e;

        }
        conexao.closeConnection();
        return e;
    }

    public boolean removerEditora(Editora editora) {
        sucess = false;
        try {

            ps = conexao.getConnection().prepareStatement(" DELETE FROM editora WHERE id = ?");

            ps.setLong(1, (editora.getId()));

            ps.executeUpdate();
            conexao.closeConnection();


            sucess = true;
        } catch (SQLException ex) {
            ex.printStackTrace();
            conexao.closeConnection();
            return sucess;
        }

        conexao.closeConnection();
        return sucess;
    }
    
    
}
